package org.xhy.sniffer.plugins.mysql;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.xhy.sniffer.interceptor.ConstructorMethodsInterceptorPoint;
import org.xhy.sniffer.interceptor.InstanceMethodsInterceptorPoint;
import org.xhy.sniffer.interceptor.StaticMethodsInterceptorPoint;
import org.xhy.sniffer.match.ClassMatch;
import org.xhy.sniffer.match.ClassNameMatch;
import org.xhy.sniffer.plugin.ClassEnhancePluginDefine;

import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.named;

public class MysqlConnectInstrumentation extends ClassEnhancePluginDefine {
    private static final String JDBC_ENHANCE_CLASS = "com.mysql.cj.jdbc.ConnectionImpl";

    private static final String INTERCEPTOR = "org.xhy.sniffer.plugins.mysql.interceptor.MySQLConnectionInterceptor";
    private static final String CONNECT_METHOD = "getInstance";



    @Override
    protected ClassMatch enhanceClass() {
        // 拦截 com.mysql.cj.jdbc.ConnectionImpl 类
        return ClassNameMatch.buildClassNameMatch(JDBC_ENHANCE_CLASS);
    }
    @Override
    protected InstanceMethodsInterceptorPoint[] getInstanceMethodsInterceptorPoints() {
        return new InstanceMethodsInterceptorPoint[]{
                new InstanceMethodsInterceptorPoint() {
                    @Override
                    public ElementMatcher<MethodDescription> getMethodsMatcher() {
                        return named(CONNECT_METHOD);
                    }

                    @Override
                    public String getMethodsInterceptor() {
                        return INTERCEPTOR;
                    }
                }
        };
    }

    @Override
    protected StaticMethodsInterceptorPoint[] getStaticMethodsInterceptorPoints() {
        return new StaticMethodsInterceptorPoint[0];
    }

    @Override
    protected ConstructorMethodsInterceptorPoint[] getConstructorMethodsInterceptorPoints() {
        return new ConstructorMethodsInterceptorPoint[0];
    }

}
